#include "convoluatedialog.h"
#include "ui_convoluatedialog.h"
#include <iostream>
using namespace std;

ConvoluateDialog::ConvoluateDialog(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::ConvoluateDialog)
{
    m_ui->setupUi(this);
    model = new QStandardItemModel(3, 3);
    nbLigne = 3;
    nbColone = 3;
    for(int i = 0; i < nbLigne; i++) {
        for(int j = 0; j < nbColone; j++) {
            model->setItem(i,j,new QStandardItem("0"));
        }
    }
    m_ui->tableView->setModel(model);

    QObject::connect(m_ui->widthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(resizeTable()));
    QObject::connect(m_ui->heightSpinBox, SIGNAL(valueChanged(int)), this, SLOT(resizeTable()));
    QObject::connect(m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(validationConvolution()));
    QObject::connect(m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

}
void ConvoluateDialog::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:


        break;
    default:
        break;
    }
}
ConvoluateDialog::~ConvoluateDialog()
{
    delete m_ui;
}

void ConvoluateDialog::resizeTable(){
    nbLigne = m_ui->widthSpinBox->value();
    nbColone = m_ui->heightSpinBox->value();

    QStandardItemModel* modelTmp = new QStandardItemModel(nbLigne,nbColone);

    for(int i = 0; i < nbLigne; i++) {
        for(int j = 0; j < nbColone; j++) {
            if(model->data(model->index(i,j)).value<QString>() != ""){
                modelTmp->setItem(i,j,new QStandardItem(model->data(model->index(i,j)).value<QString>()));
            }else{
                modelTmp->setItem(i,j,new QStandardItem("0"));
            }
        }
    }
    model = new QStandardItemModel();
    model = modelTmp;
    m_ui->tableView->setModel(model);
}

void ConvoluateDialog::validationConvolution(){
    nbLigne = m_ui->heightSpinBox->value();
    nbColone = m_ui->widthSpinBox->value();

    valeursTable = new int*[nbLigne];
    for ( int i=0 ; i < nbLigne ; i++){
        valeursTable[i] = new int [nbColone];
    }
    for(int i = 0; i < nbLigne; i++) {
        for(int j = 0; j < nbColone; j++) {
            if(model->data(model->index(i,j)).value<QString>() != ""){
                valeursTable[i][j] = model->data(model->index(i,j)).value<int>();
            }else{
                valeursTable[i][j] = 0;
            }
        }
    }
    accept();
}

